package com.itheima.service.impl;

import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 运营数据统计服务
 */
@Service
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    //查询运营数据
    public Map<String, Object> getBusinessReportData() throws Exception {
        //报表日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //本日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐查询
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String, Object> result = new HashMap<>();
        result.put("reportDate", today);
        result.put("todayNewMember", todayNewMember);
        result.put("totalMember", totalMember);
        result.put("thisWeekNewMember", thisWeekNewMember);
        result.put("thisMonthNewMember", thisMonthNewMember);
        result.put("todayOrderNumber", todayOrderNumber);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);
        result.put("todayVisitsNumber", todayVisitsNumber);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        result.put("hotSetmeal", hotSetmeal);
        return result;
    }

    @Override
    public Map getSetmealReport() {
        List<Map> setmealReport = orderDao.getSetmealReport();
        Map resultMap = new HashMap();
        List<String> setmealNames = new ArrayList<>();
        List<Object> setmealCount = new ArrayList<>();

        for (Map map : setmealReport) {
            setmealNames.add(map.get("name").toString());
            Map<String, Object> count = new HashMap<>();
            count.put("name", map.get("name").toString());
            count.put("value", map.get("count").toString());
            setmealCount.add(count);
        }
        resultMap.put("setmealNames", setmealNames);
        resultMap.put("setmealCount", setmealCount);
        return resultMap;
    }

    /**
     * 会员数量统计
     *
     * @return
     */
    @Override
    public Map<String, Object> getMemberReport() {
        Map<String, Object> map = new HashMap<>();
        //结束时间
        Date maxDate = DateUtils.getFirstDay4ThisMonth();
        //计算开始时间
        long maxTime = maxDate.getTime();
        long minTime = maxTime - (365 * 24 * 60 * 60 * 1000L);
        Date minDate = new Date(minTime);

        try {
            String minDateStr = DateUtils.parseDate2String(minDate);
            String maxDateStr = DateUtils.parseDate2String(maxDate);
            //月份列表
            List<String> monthBetween = DateUtils.getMonthBetween(minDateStr, maxDateStr, "yyyy-MM-dd");
            //2021-04-01   ~    2022-04-01
            List<String> months = new ArrayList<>();
            List<Integer> memberCounts = new ArrayList<>();
            System.out.println(monthBetween);
            for (int i = 0; i < monthBetween.size() - 1; i++) {
                Integer count = memberDao.getMemberReport(monthBetween.get(i), monthBetween.get(i + 1));
                //2021-04-01 --> 2021.04
                String originalMonth = monthBetween.get(i);//2021-04-01
                String substring = originalMonth.substring(0, 7);//2021-04
                String month = substring.replace("-", ".");
                //结果封装集合
                months.add(month);
                memberCounts.add(count);
            }
            map.put("months",months);
            map.put("memberCount",memberCounts);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

}
